

# 堆特点：heap[i] < heap[2*i+1]  & heap[i] < heap[2*i+2]
import heapq
x=[4,5,6,7,1,2,3,4,0]
print(x)
heapq.heapify(x)
print(x)

heapq.heappush(x,10)
print(x)
heapq.heappop(x)
print(x)

print("=="*100)
# 先放再弹出来
heapq.heappushpop(x,11)
print(x)
# 先弹出一个再放一个
heapq.heapreplace(x,12)
print(x)

# 将多个已排序的输入合并成一个已排序的输出，返回的是已排序的iterator
print("=="*100)
list_1 = [1,3,5,7]
list_2 = [2,4,6,8]
list_3 = heapq.merge(list_1,list_2)
print(list_3)
print(list(list_3))

print("=="*100)
# 过滤出前几个最大值、最小值
n_large = heapq.nlargest(3,x)
n_small = heapq.nsmallest(3,x)
print(n_large)
print(n_small)
